﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web.Http;
using System.Net.Http;
using System.Web.Mvc;

using ERPStore.Extensions;
using ERPStore.Web.Routing;
using ERPStore.Web.Extensions;

using Microsoft.Practices.Unity;

using EventBrokR;

namespace ERPStore.Web.Cart
{
    public class PluginInit : ERPStore.Cart.PluginInit
    {
		public PluginInit()
		{
			Container = GlobalConfiguration.Configuration.DependencyResolver.GetContainer();
		}

		Microsoft.Practices.Unity.IUnityContainer Container { get; set; }

		public override void AddServices()
		{
			base.AddServices();

			// Global Event Consumer
			var eb = Container.Resolve<EventBrokR.IPublisher>();
			eb.Container.Register<EventConsumers.UserLoggedEventConsumer>();

			// Auto Mapping
			AutoMapper.Mapper.CreateMap<ERPStore.Models.OrderCart, ERPStore.Cart.ViewModels.CartViewModel>()
				.ForMember(dest => dest.Items, opt => opt.MapFrom(source => source.Items));
			// AutoMapper.Mapper.CreateMap<List<ERPStore.Models.CartItem>, List<ERPStore.Cart.ViewModels.CartItemViewModel>>();
			AutoMapper.Mapper.CreateMap<ERPStore.Models.CartItem, ERPStore.Cart.ViewModels.CartItemViewModel>();
		}

		public override void RegisterRoutes()
		{
			RegisterOrderCartRoutes(System.Web.Routing.RouteTable.Routes);

			System.Web.Routing.RouteTable.Routes.MapHttpRoute(
				"AdminCartServiceApi"
				, "api/admincart/{action}/{*id}"
				, new { Controller = "AdminCartApi", action = "Ping", id = UrlParameter.Optional }
			);

			System.Web.Routing.RouteTable.Routes.MapHttpRoute(
				"CartServiceApi"
				, "api/cart/{action}/{*id}"
				, new { Controller = "CartApi", action = "Ping", id = UrlParameter.Optional }
			);
		}

		protected virtual void RegisterOrderCartRoutes(System.Web.Routing.RouteCollection routes)
		{
			var namespaces = new string[] { "ERPStore.Web.Cart.Controllers" };

			routes.MapERPStoreRoute(
				Routes.CART_ADD
				, "panier/ajouter/{productCode}"
				, new { controller = "Cart", action = "AddCartItem", productCode = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.CART_RECALC
				, "panier/recalculer"
				, new { controller = "Cart", action = "Recalc" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.CART_ITEM_DELETE
				, "panier/supprimer/item/{index}"
				, new { controller = "Cart", action = "RemoveItem", index = 0 }
				// , new { index = @"\d+" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.CART_CLEAR
				, "panier/vider"
				, new { controller = "Cart", action = "Clear" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.CART_DELETE
				, "panier/supprimer/{cartId}"
				, new { controller = "Cart", action = "Delete", cartId = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.CART_CHANGE
				, "panier/changer/{cartId}"
				, new { controller = "Cart", action = "Change", cartId = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.CART_SHOW
				, "panier/voir/{cartId}"
				, new { controller = "Cart", action = "Show", cartId = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.CART_ASSIGN
				, "panier/assigne/{cartId}"
				, new { controller = "Cart", action = "Assign" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.CART_CREATE
				, "panier/creation"
				, new { controller = "Cart", action = "Create" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.SHOW_ADD_TO_CART_POPUP
				, "panier/popup-ajout"
				, new { controller = "Cart", action = "ShowAddToCart" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.SHOW_CART_STATUS
				, "panier/statut"
				, new { controller = "Cart", action = "ShowStatus" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.SHOW_CURRENT_CART
				, "panier/courant"
				, new { Controller = "Cart", action = "ShowCurrentCart" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.AJAX_ADD_TO_CART
				, "panier/ajax-ajouter"
				, new { controller = "Cart", action = "JsAddItemWithQuantity" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.CART_SCRIPT
				, "panier/script"
				, new { controller = "Cart", action = "Script" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.CART_CONVERT_TO_QUOTE
				, "panier/conversion-devis/{cartId}"
				, new { controller = "Cart", action = "ConverToQuoteCart", cartId = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.LAST_CART_ITEM_LIST
				, "panier/derniers-produits"
				, new { controller = "Cart", action = "ShowLastCartItemList" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.CART_CROSS_SELLING
				, "panier/cross-selling"
				, new { controller = "Cart", action = "ShowCrossSellingByCart" }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.CART
				, "panier"
				, new { controller = "Cart", action = "Index", id = string.Empty }
				, namespaces
			);

			routes.MapERPStoreRoute(
				Routes.DEFAULT_CART
				, "panier/{action}/{id}"
				, new { controller = "Cart", action = "Index", id = "" }  // Parameter defaults
				, namespaces
			);
		}

    }
}
